package com.clwl.supervise.superviseapp.service.pro;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clwl.supervise.superviseapp.entity.base.SysDictData;
import com.clwl.supervise.superviseapp.entity.base.TsUser;
import com.clwl.supervise.superviseapp.entity.pro.*;
import com.clwl.supervise.superviseapp.mapper.base.SysDictDataMapper;
import com.clwl.supervise.superviseapp.mapper.base.TsUserMapper;
import com.clwl.supervise.superviseapp.mapper.pro.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service
public class JgProDynamicScoreService extends ServiceImpl<JgProDynamicScoreMapper, JgProDynamicScore> {

    @Resource
    private JgProDailyBonusPointsItemMainMapper jgProDailyBonusPointsItemMainMapper;

    @Resource
    private JgProDynamicScoreBonusPointsItemMapper jgProDynamicScoreBonusPointsItemMapper;

    @Resource
    private JgProDailyRandomCertFlightContentMainMapper jgProDailyRandomCertFlightContentMainMapper;

    @Resource
    private JgProDynamicScoreRandomCertFlightContentMapper jgProDynamicScoreRandomCertFlightContentMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private TsUserMapper userMapper;

    @Transactional
    public boolean dynamicScore(JgProDynamicScore jgProDynamicScore){
        if(this.baseMapper.insert(jgProDynamicScore)>0){
            //查询加分项数据
            List<JgProDailyBonusPointsItemMain> bonusPointsItemMainList = jgProDailyBonusPointsItemMainMapper.selectList(new QueryWrapper<>());
            for(JgProDailyBonusPointsItemMain bonusPointsItemMain : bonusPointsItemMainList){
                JgProDynamicScoreBonusPointsItem item = new JgProDynamicScoreBonusPointsItem();

                BeanUtil.copyProperties(bonusPointsItemMain,item,"id");
                item.setDynamicScoreId(jgProDynamicScore.getId());
                item.setItemId(bonusPointsItemMain.getId());
                if(Arrays.asList(jgProDynamicScore.getPlusesIds()).contains(bonusPointsItemMain.getId())){
                    item.setChecked("1");
                } else {
                    item.setChecked("0");
                }
                jgProDynamicScoreBonusPointsItemMapper.insert(item);
            }
            //查询评分要素
            List<JgProDailyRandomCertFlightContentMain> randomCertFlightContentMainList = jgProDailyRandomCertFlightContentMainMapper.selectList(new QueryWrapper<>());
            for(JgProDailyRandomCertFlightContentMain randomCertFlightContentMain : randomCertFlightContentMainList){
                JgProDynamicScoreRandomCertFlightContent content = new JgProDynamicScoreRandomCertFlightContent();

                BeanUtil.copyProperties(randomCertFlightContentMain,content,"id");
                content.setDynamicScoreId(jgProDynamicScore.getId());
                content.setContentId(randomCertFlightContentMain.getId());
                //根据分值判断选中了哪个，如果有分数相同的选项 可能会有问题，但是用ID标识太麻烦了 前端框架太垃圾，先这样吧。
                if(randomCertFlightContentMain.getItemId().intValue() == 13 && randomCertFlightContentMain.getRiskScore() == jgProDynamicScore.getRandomPublicScore()){
                    content.setSelected("1");
                } else if(randomCertFlightContentMain.getItemId().intValue() == 14 && randomCertFlightContentMain.getRiskScore() == jgProDynamicScore.getCertExamScore()){
                    content.setSelected("1");
                } else if(randomCertFlightContentMain.getItemId().intValue() == 15 && randomCertFlightContentMain.getRiskScore() == jgProDynamicScore.getFlightCheckScore()){
                    content.setSelected("1");
                } else {
                    content.setSelected("0");
                }
                jgProDynamicScoreRandomCertFlightContentMapper.insert(content);
            }

            return true;
        } else {
            return false;
        }
    }


    public List<SysDictData> selectByDictTypeCode(String dictTypeCode){
        return sysDictDataMapper.selectByDictTypeCode(dictTypeCode);
    }

    public List<TsUser> selectByDeptId(Long deptId){
        List<TsUser> userList = userMapper.selectByDeptId(deptId);
        for(TsUser user : userList){
            user.setText(user.getName());
            user.setValue(user.getId());
        }
        return userList;
    }
}
